﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using WBGModel;
using System.Data;

namespace WBGDal
{
    public class DalWebMessage : ICRUD
    {
        public List<ModelWebMessage> Listar()
        {
            return Listar("");
        }

        public List<ModelWebMessage> Listar(string filtro)
        {
            String cmd = "SELECT * FROM [WBG].[WebMessages]" + (filtro.CompareTo("") == 0 ? "" : " WHERE " + filtro);
            return ConvertToList(UtilBanco.ExecComRetorno(cmd));
        }

        private List<ModelWebMessage> ConvertToList(DataTable resultSet)
        {
            List<ModelWebMessage> lst = new List<ModelWebMessage>();
            for (int i = 0; i < resultSet.Rows.Count; i++)
            {
                DataRow dr = resultSet.Rows[i];
                ModelWebMessage msg = new ModelWebMessage();
                msg.idMessage = Convert.ToInt32(dr["idMessage"]);
                msg.type = Convert.ToInt16(dr["type"]);
                msg.answered = Convert.ToBoolean(dr["answered"]);
                msg.dateMessage = Convert.ToDateTime(dr["dateMessage"]);
                msg.personName = dr["personName"].ToString();
                msg.personEmail = dr["personEmail"].ToString();
                msg.bodyMessage = dr["bodyMessage"].ToString();
                lst.Add(msg);
            }
            return lst;
        }
        public void Inserir(ModelWebMessage obj)
        {
            String cmd = String.Format("INSERT INTO [WBG].[WebMessages] (personEmail,personName,answered,bodyMessage,type) VALUES ('{0}','{1}','{2}','{3}',{4})", obj.personEmail, obj.personName, obj.answered, obj.bodyMessage, obj.type);
            UtilBanco.ExecSemRetorno(cmd);
        }

        public void Alterar(ModelWebMessage obj)
        {
            String cmd = String.Format("UPDATE [WBG].[WebMessages] SET personEmail='{0}',personName='{1}',answered='{2}',bodyMessage='{3}',type={4} WHERE idMessage={5}", obj.personEmail, obj.personName, obj.answered, obj.bodyMessage, obj.type, obj.idMessage);
            UtilBanco.ExecSemRetorno(cmd);
        }

        public void Deletar(int id)
        {
            String cmd = String.Format("DELETE FROM [WBG].[WebMessages] WHERE idMessage={0}", id);
            UtilBanco.ExecSemRetorno(cmd);
        }
    }
}
